package com.situ.staff.controller;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.situ.staff.bean.JsonResult;
import com.situ.staff.bean.Project;
import com.situ.staff.service.ProjectService;

@Controller
@RequestMapping("/project")
public class ProjectController {

	@Autowired
	ProjectService projectService;

	@GetMapping("/add")
	public String add() {

		return "project/add";
	}

	@PostMapping("/add")
	public String add(Project project, @RequestParam("staff_id") ArrayList<Integer> staff_ids, Model model) {

		try {
			int result = projectService.add(project, staff_ids);
			return "redirect:list";

		} catch (Exception e) {
			e.printStackTrace();
			model.addAttribute("error", e.getMessage());
			return "project/add";
		}

	}

	@PostMapping("/batchDelete")
	@ResponseBody
	public JsonResult batchDelete(@RequestParam(value = "ids[]") ArrayList<Integer> ids) {

		int result = projectService.batchDelete(ids);

		JsonResult jsonResult = new JsonResult();
		jsonResult.setCode(result);

		return jsonResult;
	}

	@GetMapping("/list")
	public String List() {
		return "project/list";

	}

	@PostMapping("/list")
	@ResponseBody
	public JsonResult listJson(Integer page, Integer limit, Project project) {

		PageInfo forPage = projectService.getForPage(page, limit, project);
		JsonResult jsonResult = new JsonResult();
		jsonResult.setCode(0);
		jsonResult.setCount((int) forPage.getTotal());
		jsonResult.setData(forPage.getList());

		return jsonResult;
	}

}
